/*
 * author      : dormi330
 * date        : 2018/6/24
 * description : 订单相关操作 和 查询
 */

package com.deep.trade.application.refund;

import com.deep.trade.adaptor.required.IdService;
import com.deep.trade.application.RefundFacadeImpl;
import com.deep.trade.application.order.OrderBaseOperation;
import com.deep.trade.order.dto.OutDtoTradeOrder;
import com.deep.trade.order.dto.OutDtoTradeOrderWithSku;
import com.deep.trade.refund.dto.InDtoRefundApply;
import com.deep.trade.refund.dto.InDtoRefundType;
import com.deep.trade.refund.dto.OutDtoRefundType;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;

public class RefundBaseOperation extends OrderBaseOperation {

    protected final String catalogId = IdService.CATALOG_ID;

    @Autowired
    protected RefundFacadeImpl refundFacadeImpl;

    protected void addRefundType() throws Exception {
        refundFacadeImpl.addNewRefundType(new InDtoRefundType(catalogId, 1L, "其他"));
        refundFacadeImpl.addNewRefundType(new InDtoRefundType(catalogId, 2L, "质量问题"));
        refundFacadeImpl.addNewRefundType(new InDtoRefundType(catalogId, 3L, "买错了"));
        refundFacadeImpl.addNewRefundType(new InDtoRefundType(catalogId, 4L, "快递问题"));
    }

    protected List<OutDtoRefundType> listAllRefundType() throws Exception {
        return refundFacadeImpl.listAllRefundType();
    }

    protected void preparePaidOrder() throws Exception {
        // 下单
        super.placeOrder();
        // 审核全部
        List<OutDtoTradeOrder> outDtoTradeOrderList = super.tradeOrderListOfMember();
        super.auditTradeOrderAllPass(outDtoTradeOrderList);
        // 支付全部
        for (OutDtoTradeOrder aOrder : super.tradeOrderListOfMember()) {
            super.payTradeOrder(aOrder);
        }
    }

    protected List<OutDtoTradeOrderWithSku> tradeOrderWithSkuListOfMember() throws Exception {
        return orderFacadeImpl
                .tradeOrderWithSkuListOfMember(pageNum, pageSize)
                .getList();
    }

    protected InDtoRefundApply applyRefund() throws Exception {
        List<OutDtoRefundType> typeList = listAllRefundType();
        OutDtoRefundType refundType1 = typeList.get(0);

        List<OutDtoTradeOrderWithSku> orderListWithSku = tradeOrderWithSkuListOfMember();
        assertNotNull(orderListWithSku);
        assertEquals(2, orderListWithSku.size());

        // 找出一个进行退款
        OutDtoTradeOrderWithSku one = orderListWithSku.get(0);

        InDtoRefundApply apply = new InDtoRefundApply();
        apply.setTradeOrderId(one.getId());
        apply.setDescription("测试退款");
        // apply.setRefundMoney(null);
        // apply.setSkuIds(null);
        apply.setRefundTypeId(refundType1.getId());

        refundFacadeImpl.applyRefund(apply);
        return apply;
    }
}
